﻿namespace ufida.u9.ait.openapi
{
	using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.FA.FA_AssetCardBE;
    using UFIDA.U9.MO.MO;
    using UFIDA.U9.PM.Rcv;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Business;
    using UFSoft.UBF.Transactions;
    using UFSoft.UBF.Util.Context;
    using UFSoft.UBF.Util.Data;

    /// <summary>
    /// RCVSaveCardSV partial 
    /// </summary>	
	public partial class RCVSaveCardSV 
	{	
		internal BaseStrategy Select()
		{
			return new RCVSaveCardSVImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class RCVSaveCardSVImpementStrategy : BaseStrategy
	{
		public RCVSaveCardSVImpementStrategy() { }

		public override object Do(object obj)
		{						
			RCVSaveCardSV bpObj = (RCVSaveCardSV)obj;

            UBFTransactionScope scope = null;
            bool useTrans = true;
            Receivement doc = null;
            List<CopyOfOBAReceivementDTOData> dtoData = bpObj.Data;

            try
            {
                //启用UBF事务
                if (useTrans) scope = new UBFTransactionScope(TransactionOption.RequiresNew);

                if (dtoData == null || dtoData.Count == 0)
                    throw new Exception("传入参数为空");
                List<Hashtable> retlist = new List<Hashtable>();

                foreach (var dto in dtoData)
                {
                    //查询单据实体
                    String path = DTOUtils.GetDocNoPath(dto);
                    EntityQuery query = new EntityQuery(typeof(Receivement).FullName);
                    System.Collections.IList list = query.FindAll(path);

                    if (list == null || list.Count <= 0)
                    {
                        //新增逻辑
                        doc = RCVBase.CreateDoc(dto);
                        //修改单号
                        EntityUtils.UpdateDocNo<Receivement>(doc.ID, ReflexUtils.GetValue("DocNo", dto));
                        //审核
                        RCVBase.ApproveDoc(doc);
                        //生成资产卡片
                        List<AssetCard> cards = RCVBase.RcvCreateCard(doc, dto);

                        //返回值
                        List<Hashtable> cardlist = new List<Hashtable>();
                        foreach(var card in cards)
                        {
                            Hashtable retcard = new Hashtable();
                            retcard.Add("CardNo", card.DocNo);
                            retcard.Add("AssetTag", card.AssetTag[0].Code);
                            cardlist.Add(retcard);
                        }
                        doc = doc.Key.GetEntity();
                        Hashtable ret = new Hashtable();
                        ret.Add("ID", doc.ID);
                        ret.Add("DocNo", doc.DocNo);
                        ret.Add("TagList", cardlist);
                        retlist.Add(ret);
                    }
                    else
                    {
                        //修改逻辑
                        throw new AITException(599, $"单号:{DTOUtils.GetDocNo(dto)}已经存在,不能生成!");
                    }
                }

                //整体事务提交
                if (scope != null) scope.Commit();
                return ResultUtils.success(bpObj, dtoData, retlist);
            }
            catch (Exception ex)
            {
                //事务回滚
                if (scope != null) scope.Rollback();

                return ResultUtils.error(bpObj, dtoData, ex);
            }
            finally
            {
                //查看源码 Release()和Dispose()作用相同
                if (scope != null) scope.Release();
            }
        }		
	}

	#endregion
	
	
}